﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

namespace ObjectMapper.Benchmark
{
    public class ExecutionResult
    {
        private IDictionary<string, TimeSpan> _durations;

        public ExecutionResult(int recordCount)
        {
            RecordCount = recordCount;
        }

        public IDictionary<string, TimeSpan> Durations
        {
            get { return _durations ?? (_durations = new Dictionary<string, TimeSpan>()); }
        }
        public int RecordCount { get; private set; }

        public static void WriteConsoleHeader()
        {
            Console.Write("Records  ");
            foreach (var executor in Program.Executors)
            {
                var name = executor.Name;
                if (name.Length > 14)
                {
                    name = name.Substring(0, 12) + "~1";
                }
                Console.Write(name.PadRight(15, ' '));
            }
            Console.WriteLine();
        }

        public virtual void WriteConsoleRow()
        {
            Console.Write(RecordCount.ToString(CultureInfo.InvariantCulture).PadLeft(8, ' ') + " ");
            foreach (var executor in Program.Executors)
            {
                Console.Write(Durations[executor.Name].TotalMilliseconds.ToString("F2").PadLeft(14, ' ') + " ");
            }
            Console.WriteLine();
        }

        public static void WriteCsvHeader(StreamWriter writer)
        {
            writer.Write("Records");
            foreach (var executor in Program.Executors)
            {
                writer.Write(",");
                writer.Write(executor.Name);
            }
            writer.WriteLine();
        }

        public virtual void WriteCsvRow(StreamWriter writer)
        {
            writer.Write(RecordCount.ToString(CultureInfo.InvariantCulture));
            foreach (var executor in Program.Executors)
            {
                writer.Write(",");
                writer.Write(Durations[executor.Name].TotalMilliseconds.ToString("F4"));
            }
            writer.WriteLine();
        }
    }
}
